﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using Provausio.Common.Data;
using Provausio.Common.Objects;
using TrainingManagement.api.Models.BusinessObjects;

namespace TrainingManagement.api.Models.Data.Readers
{
    public class DepartmentTreeSelectByLocationDataSource : QuerySource<int, List<DepartmentTree>>
    {
        public override List<DepartmentTree> Execute(int request)
        {
            const string selectByLocation = "dbo.training_api_DepartmentTreeSelectByLocation";
            var parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@LocationID", SqlDbType.Int){ Value = request};

            using (var rdr = SqlHelper.ExecuteReader(
                ConnectionStrings.TrainingDb,
                CommandType.StoredProcedure,
                selectByLocation,
                parameters))
            {
                var results = new List<DepartmentTree>();
                var treeReader = new DepartmentTreeDataReader();
                while (rdr.Read())
                {
                    results.Add(treeReader.Run(rdr));
                }
                return results;
            }
        }
    }
}